home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / scripts / polynomial / compan.m next >
Text File  |  1997-03-07  |  2KB  |  75 lines

  1. ## Copyright (C) 1996 John W. Eaton
  2. ##
  3. ## This file is part of Octave.
  4. ##
  5. ## Octave is free software; you can redistribute it and/or modify it
  6. ## under the terms of the GNU General Public License as published by
  7. ## the Free Software Foundation; either version 2, or (at your option)
  8. ## any later version.
  9. ##
  10. ## Octave is distributed in the hope that it will be useful, but
  11. ## WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. ## General Public License for more details.
  14. ##
  15. ## You should have received a copy of the GNU General Public License
  16. ## along with Octave; see the file COPYING.  If not, write to the Free
  17. ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  18. ## 02111-1307, USA.
  19.  
  20. ## usage: compan (c)
  21. ##
  22. ## Compute the companion matrix corresponding to polynomial vector c.
  23. ##
  24. ## In octave a polynomial is represented by it's coefficients (arranged
  25. ## in descending order). For example a vector c of length n+1 corresponds
  26. ## to the following nth order polynomial
  27. ##
  28. ##   p(x) = c(1) x^n + ... + c(n) x + c(n+1).
  29. ##
  30. ## The corresponding companion matrix is
  31. ##         _                                                        _
  32. ##        |  -c(2)/c(1)   -c(3)/c(1)  ...  -c(n)/c(1)  -c(n+1)/c(1)  |
  33. ##        |       1            0      ...       0             0      |
  34. ##        |       0            1      ...       0             0      |
  35. ##    A = |       .            .   .            .             .      |
  36. ##        |       .            .       .        .             .      |
  37. ##        |       .            .           .    .             .      |
  38. ##        |_      0            0      ...       1             0     _|
  39. ##
  40. ## The eigenvalues of the companion matrix are equal to the roots of the
  41. ## polynomial.
  42. ##
  43. ## SEE ALSO: poly, roots, residue, conv, deconv, polyval, polyderiv, polyinteg
  44.  
  45. ## Author: Tony Richardson <amr@mpl.ucsd.edu>
  46. ## Created: June 1994
  47. ## Adapted-By: jwe
  48.  
  49. function A = compan (c)
  50.  
  51.   if (nargin != 1)
  52.     usage ("compan (vector)");
  53.   endif
  54.  
  55.   if (! is_vector (c))
  56.     error("compan: expecting a vector argument.");
  57.   endif
  58.  
  59.   ## Ensure that c is a row vector.
  60.  
  61.   if (rows (c) > 1)
  62.     c = c.';
  63.   endif
  64.  
  65.   n = length (c);
  66.  
  67.   if (n == 1)
  68.     A = [];
  69.   else
  70.     A = diag (ones (n-2, 1), -1);
  71.     A(1,:) = -c(2:n) / c(1);
  72.   endif
  73.  
  74. endfunction
  75.